public class Main {
    private static void registerApis(HttpServer server) {
        server.get("/", (request, response) -> {
            System.out.println("\n起始页逻辑开始");
            JsonResponse jsonResponse = new JsonResponse();
            jsonResponse.addProperty("message", "欢迎使用！");
            jsonResponse.addProperty("status", "success");
            jsonResponse.addProperty("timestamp", System.currentTimeMillis());
            response.sendJson(200, jsonResponse);
            System.out.println("\n起始页逻辑结束");
        });

        // 获取用户信息接口
        server.get("/api/users/{id}", (request, response) -> {
            String userId = request.getPathParameter("id");
            JsonResponse jsonResponse = new JsonResponse();
            jsonResponse.addProperty("id", userId);
            jsonResponse.addProperty("name", "用户" + userId);
            jsonResponse.addProperty("email", "user" + userId + "@example.com");
            jsonResponse.addProperty("createdAt", "2024-01-01");
            response.sendJson(200, jsonResponse);
        });

        // 获取用户列表接口
        server.get("/api/users", (request, response) -> {
            System.out.println("\n/api/users 逻辑开始");
            JsonResponse users = new JsonResponse();
            users.addProperty("total", 3);

            JsonResponse user1 = new JsonResponse();
            user1.addProperty("id", "1");
            user1.addProperty("name", "张三");

            JsonResponse user2 = new JsonResponse();
            user2.addProperty("id", "2");
            user2.addProperty("name", "李四");

            JsonResponse user3 = new JsonResponse();
            user3.addProperty("id", "3");
            user3.addProperty("name", "王五");

            System.out.println("haha");
            users.addProperty("users", new JsonResponse[]{user1, user2, user3});
            response.sendJson(200, users);
            System.out.println("\n/api/users 逻辑结束");
        });

        // 创建用户接口
        server.post("/api/users", (request, response) -> {
            try {
                String body = request.getBody();
                JsonResponse requestData = JsonResponse.parse(body);

                String name = requestData.getPropertyString("name");
                String email = requestData.getPropertyString("email");

                JsonResponse jsonResponse = new JsonResponse();
                jsonResponse.addProperty("id", "100");
                jsonResponse.addProperty("name", name);
                jsonResponse.addProperty("email", email);
                jsonResponse.addProperty("message", "用户创建成功");
                jsonResponse.addProperty("createdAt", System.currentTimeMillis());

                response.sendJson(201, jsonResponse);
            } catch (Exception e) {
                JsonResponse errorResponse = new JsonResponse();
                errorResponse.addProperty("error", "无效的请求数据");
                errorResponse.addProperty("message", e.getMessage());
                response.sendJson(400, errorResponse);
            }
        });

        // 健康检查接口
        server.get("/health", (request, response) -> {
            JsonResponse jsonResponse = new JsonResponse();
            jsonResponse.addProperty("status", "healthy");
            jsonResponse.addProperty("timestamp", System.currentTimeMillis());
            response.sendJson(200, jsonResponse);
        });
    }

    public static void main(String[] args) {
        HttpServer server = new HttpServer(8080);

        // 注册示例API接口
        registerApis(server);

        // 添加关闭钩子
        Runtime.getRuntime().addShutdownHook(new Thread(server::stop));

        try {
            server.start();
        } catch (Exception e) {
            System.err.println("服务器运行出错: " + e.getMessage());
            server.stop();
        }
    }
}
